package com.vf.s.mvc.biz.controller;


import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.core.controller.BaseController;
import com.vf.core.util.UuidUtil;
import com.vf.s.common.model.biz.BizLayer;
import com.vf.s.common.model.biz.BizMap;
import com.vf.s.common.model.biz.BizSceneMap;
import com.vf.s.common.plugins.shiro.ShiroKit;
import com.vf.s.common.plugins.shiro.SimpleUser;

public class BizMapController extends BaseController {
	
	public void index() {
		this.render("list.html");
	}
	
	public void mapForEdit() {
		this.set("sceneId", getPara("sceneId"));
		this.render("mapForEdit.html");
	}
	
	public void edit() {
		this.set("sceneId", getPara("sceneId"));
		this.set("layerId", getPara("layerId"));
		this.render("edit.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String name = getPara("name");
		String dataType = getPara("dataType");
		String isPublic = getPara("isPublic");
		
		String userId="";
		SimpleUser simpleUser = ShiroKit.getLoginUser();
		if (simpleUser != null) {
			userId=simpleUser.getId();
		}
		
		String select = " select *  ";
		String sqlExceptSelect = " from " + BizMap.TABLE_NAME + " o  where 1=1 ";
		if (!StrKit.isBlank(name)) {
			sqlExceptSelect += " and o.name like '%" + name + "%'  ";
		}
		if (!StrKit.isBlank(dataType)) {
			sqlExceptSelect += " and o.dataType = '" + dataType + "'  ";
		}

		if(StrKit.equals("1",isPublic)) {
			sqlExceptSelect += " and o.createUserId = '" + userId + "'   ";	
		}
		else if(StrKit.equals("0",isPublic)) {
			sqlExceptSelect += " and o.isPublic = '0'   ";	
		}else {
			if(!StrKit.equals(simpleUser.getUsername(),"system")) {
				sqlExceptSelect += " and ( o.createUserId = '" + userId + "' or  o.isPublic = '0' ) ";
			}
		}
		
		Page<BizMap> page = BizMap.dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	public void save() {
		BizMap model = getModel(BizMap.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			SimpleUser simpleUser = ShiroKit.getLoginUser();
			if (simpleUser != null) {
				model.setCreateUserId(simpleUser.getId());
				model.setCreateUserName(simpleUser.getName());
			}
			if (model.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}

	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() {
		BizMap model = BizMap.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	/**
	 * 底图数据与图层数据关联
	 */
	public void addMapToLayer() {
		String mapId=getPara("id");
		String sceneId=getPara("sceneId");
		String layerId=getPara("layerId");
		BizSceneMap bizSceneMap=BizSceneMap.dao.findFirst("SELECT * FROM "+BizSceneMap.TABLE_NAME+" WHERE sceneId=? AND layerId=? AND MAPID=?",sceneId,layerId,mapId);
		if(bizSceneMap==null) {
			bizSceneMap=new BizSceneMap();
			bizSceneMap.setId(UuidUtil.getUUID());
			bizSceneMap.setSceneId(sceneId);
			bizSceneMap.setMapId(mapId);
			bizSceneMap.setLayerId(layerId);
			if(bizSceneMap.save()) {
				renderSuccess("成功");
			}else {
				renderError("失败!");
			}
		}else {
			renderError("图层已经存在!");
		}
	}
	
	public void getById() {
		BizLayer bizLayer=BizLayer.dao.findById(getPara("layerId"));
		if(bizLayer!=null) {
			BizMap bizMap = BizMap.dao.findById(bizLayer.getDataId());
			if (bizMap != null) {
				bizMap.setId(bizLayer.getId());
				renderSuccess("成功！",bizMap);
			} else {
				renderError("数据不存在,请刷新后再试!");
			}
		}else {
			renderError("数据不存在,请刷新后再试!");
		}
		
		
	}
}
